using System;
using System.Collections.Generic;
using System.Text;

using MeetCommon.Schemas;

namespace MeetCommon.Service
{
	public class MVESettings
	{
		private MVESettingsDataset dsSettings = new MVESettingsDataset();
		private SettingsIO sio;

		public MVESettings(string filename)
		{
			sio = new SettingsIO(dsSettings);
			sio.Filename = filename;
			sio.Load();
		}

		public MVESettings()
		{
			sio = new SettingsIO(dsSettings);
			sio.Filename = "AppSettings.config";
			sio.Load();
		}

		#region ACCESS_METHODS

		public string GetText(string name)
		{
			MVESettingsDataset.ElementsRow row = dsSettings.Elements.FindByName(name);
			if (row != null)
				return row.Value;
			else
				return "";
		}

		public void SetText(string name, string value)
		{
			MVESettingsDataset.ElementsRow row;
			row = dsSettings.Elements.FindByName(name);
			if (row != null)
				row.Value = value;
			else
				row = dsSettings.Elements.AddElementsRow(name, value);

			sio.Save();
		}

		public int GetInt(string name, int nDefault)
		{
			string val = GetText(name);
			if (val != "")
				return Int32.Parse(val);
			else
				return nDefault;
		}

		public int GetInt(string name)
		{
			return GetInt(name, 0);
		}

		public void SetInt(string name, int value)
		{
			SetText(name, value.ToString());
		}

		public bool GetFlag(string name, bool fDefault)
		{
			string val = GetText(name);
			if (val == Boolean.TrueString)
				return true;
			else if (val == Boolean.FalseString)
				return false;
			else
				return fDefault;
		}

		public bool GetFlag(string name)
		{
			return GetFlag(name, false);
		}

		public void SetFlag(string name, bool value)
		{
			SetText(name, value.ToString());
		}

		#endregion
	}
}
